package com.qhy.springcloud.controller;

import com.qhy.springcloud.api.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @author qiuhongyu
 * @date 2020/9/25 18:25
 */
@RestController
public class DeptController {
    private static final String HOST_URL_PREXY = "http://SPRINGCLOUD-PROVIDER-DEPT";
    @Autowired
    private RestTemplate restTemplatel;

    /**
     * 新增部门信息
     *
     * @param dept
     * @return
     */
    @PostMapping("addDept")
    public boolean addDept(Dept dept) {
        return true;
    }

    /**
     * 根据部门id查询部门信息
     *
     * @param id
     * @return
     */
    @GetMapping("/queryDept/{id}")
    public Dept queryDept(@PathVariable("id") Long id) {
        String url = HOST_URL_PREXY + "/queryDept/" + id;
        System.out.println(url);
        return restTemplatel.getForObject(url, Dept.class);
    }

    /**
     * 查询所有的部门信息
     *
     * @return
     */
    @GetMapping("queryDepts")
    public List<Dept> queryDepts() {
        String url = HOST_URL_PREXY + "/queryDepts";
        return restTemplatel.getForObject(url, List.class);
    }
}
